package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.AdminPass;
import entities.ConnectionManager;

public class AdminPassDA {
	public AdminPass[] selectAdminPasses(AdminPass adminPass)
	{
		boolean whereClause = false;
		String query = "SELECT * from AdminPass  " ;
		if(adminPass.getAdminID() != -1)
		{
			query += "WHERE adminID = " + adminPass.getAdminID();
			whereClause = true;
		}

		if(!adminPass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + adminPass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + adminPass.getPasswordHash() + "'";
			whereClause = true;
		}
		if(!adminPass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND Salt = '" + adminPass.getSalt() + "'";
			else
				query += " WHERE Salt = '" + adminPass.getSalt() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<AdminPass> adminPasses = new ArrayList<AdminPass>();

		
		try {
			while(rs.next())
			{
				AdminPass temp= new AdminPass();
				temp.setAdminID(rs.getInt("adminid"));
				temp.setPasswordHash(rs.getString("passwordHash"));
				temp.setSalt(rs.getString("salt"));
				adminPasses.add(temp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AdminPass [] temp = new AdminPass [0];
		return adminPasses.toArray(temp);

	}


	public boolean updateAdminPass(AdminPass adminPass)
	{
		String query = "UPDATE AdminPass SET " ;
		if(!adminPass.getPasswordHash().equals("")) 
		{
			query += " passwordHash = '" + adminPass.getPasswordHash() + "'";
			if(!adminPass.getSalt().equals("")) 
			{
				query += " , salt = '" + adminPass.getSalt() + "'";
			}
		}
		else
		{
			if(!adminPass.getSalt().equals("")) 
			{
				query += " salt = '" + adminPass.getSalt() + "'";
			}
		}
		
		query += " WHERE adminID = "+ adminPass.getAdminID() ;
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertAdminPass(AdminPass adminPass)
	{
		String query = "INSERT INTO AdminPass ( adminID , PasswordHash , salt) VALUES " +
				"(" + adminPass.getAdminID() + " ,  '"+ adminPass.getPasswordHash()+ "' , '" + adminPass.getSalt()+ "' )";
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteAdminPass(AdminPass adminPass)
	{
		boolean whereClause = false;
		String query = "DELETE FROM AdminPass ";
		if(adminPass.getAdminID() != -1)
		{
			query += "WHERE adminID = " + adminPass.getAdminID();
			whereClause = true;
		}
		
		if(!adminPass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + adminPass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + adminPass.getPasswordHash() + "'";
			whereClause = true;
		}
		
		if(!adminPass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND salt = '" + adminPass.getSalt() + "'";
			else
				query += " WHERE salt = '" + adminPass.getSalt() + "'";
			whereClause = true;
		}
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		AdminPass u = new AdminPass();

		
		
		u.setPasswordHash("a");
		u.setAdminID(1);
		u.setSalt("sadsadsad");
		
		AdminPassDA da = new AdminPassDA();
		System.out.println(da.deleteAdminPass(u));
	}

}
